extends Node

enum Sound {
	BOUNCE,
	HURT,
	PASS,
	POWER_SHOT,
	SHOOT,
	TACKLE,
	UI_NAVIGATE,
	UI_SELECT,
	WHISTLE
}

const SFX_MAP: Dictionary[Sound, AudioStream] = {
	Sound.BOUNCE : preload("uid://debaceo6kt8ep"),
	Sound.HURT : preload("uid://c6trtp8m3ikd4"),
	Sound.PASS : preload("uid://62ahwkgdjmqq"),
	Sound.POWER_SHOT : preload("uid://d304cevx213i2"),
	Sound.SHOOT : preload("uid://kx08apnby4fa"),
	Sound.TACKLE : preload("uid://bspdhu272ptgb"),
	Sound.UI_NAVIGATE : preload("uid://b0vhf3bmipaur"),
	Sound.UI_SELECT : preload("uid://dbubr1ngpu35k"),
	Sound.WHISTLE : preload("uid://caruohby5e1c3")
}

const NB_CHANNELS := 4

var stream_players: Array[AudioStreamPlayer] = []

func _ready() -> void:
	for i in range(NB_CHANNELS):
		var stream_player := AudioStreamPlayer.new()
		stream_players.append(stream_player)
		add_child(stream_player)
		
func find_first_available_player() -> AudioStreamPlayer:
	for stream_player in stream_players:
		if not stream_player.playing:
			return stream_player
	return null
	
func play(sound: Sound) -> void:
	var stream_player := find_first_available_player()
	if stream_player != null:
		stream_player.stream = SFX_MAP[sound]
		stream_player.play()
